<template>
  <div>
    <el-button type="primary" @click="openDialog()">新增日志</el-button>
    <el-button type="danger" @click="test">测试</el-button>
    <el-table :data="logs" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="id" label="ID" width="60"/>
      <el-table-column prop="product_id" label="商品ID"/>
      <el-table-column prop="change_quantity" label="变动数量"/>
      <el-table-column prop="change_reason" label="变动原因"/>
      <el-table-column prop="change_time" label="变动时间"/>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="openDialog(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteLog(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-model:current-page="page"
      :page-size="pageSize"
      :total="total"
      @current-change="fetchLogs"
      layout="total, prev, pager, next"
      style="margin-top: 20px;"
    />
    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="form.id ? '编辑日志' : '新增日志'">
      <el-form :model="form" label-width="100px">
        <el-form-item label="商品ID">
          <el-input v-model="form.product_id"/>
        </el-form-item>
        <el-form-item label="变动数量">
          <el-input v-model="form.change_quantity"/>
        </el-form-item>
        <el-form-item label="变动原因">
          <el-input v-model="form.change_reason"/>
        </el-form-item>
        <el-form-item label="变动时间">
          <el-date-picker v-model="form.change_time" type="datetime" style="width: 100%;"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="saveLog">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {ref, onMounted} from 'vue'
import {ElMessage} from 'element-plus'
import axios from 'axios'

const logs = ref([])
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)
const dialogVisible = ref(false)
const form = ref({
  id: null,
  product_id: '',
  change_quantity: '',
  change_reason: '',
  change_time: ''
})

function fetchLogs() {
  axios.get('/log/list', {params: {page: page.value, page_size: pageSize.value}})
    .then(res => {
      logs.value = res.data
      total.value = res.data.total
    })
}
function openDialog(row = null) {
  if (row) {
    form.value = {...row}
  } else {
    form.value = {id: null, product_id: '', change_quantity: '', change_reason: '', change_time: ''}
  }
  dialogVisible.value = true
}
function saveLog() {
  const url = form.value.id ? '/log/update' : '/log/add'
  axios.post(url, form.value).then(() => {
    ElMessage.success('保存成功')
    dialogVisible.value = false
    fetchLogs()
  })
}
function deleteLog(id) {
  axios.post('/log/delete?id='+id).then(() => {
    ElMessage.success('删除成功')
    fetchLogs()
  })
}
onMounted(fetchLogs)
</script>

<style scoped>

</style>